package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (C), 2021-2022
 * Author: 16372
 * Date: 2022/2/25 20:11
 * FileName: ArticleController
 * Description: 文章管理
 */
@RestController
@RequestMapping("/ydd_article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /*@GetMapping("/list")
    public ResultData getListByPage(Long satrt_time,
                                    Long end_time,
                                    String title,
                                    Integer cateid,
                                    Integer page,
                                    Integer limit){

        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(title != null && title != "",Article::getTitle,title)
                .eq(cateid != null, Article::getCateid,cateid)
                .between(satrt_time != null && end_time != null,Article::getCreate_date,satrt_time,end_time);
        Page<Article> articlePage = new Page<>(page,limit);
        articleService.page(articlePage,lambdaQueryWrapper);

        return ResultData.ok("", PageResult.init(articlePage));
    }*/

    /**
     * @Author 16372
     * @Description //TODO 文章分页列表查询
     * @Date 14:04 2022/2/26
     * @Param [start_time, end_time, title, cateid, page, limit]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/list")
    public ResultData getListByPage(Long start_time,
                                    Long end_time,
                                    String title,
                                    Integer cateid,
                                    Integer page,
                                    Integer limit){
        System.out.println(start_time+ "==========="+end_time);
        MPJLambdaWrapper<Article> lambdaWrapper = new MPJLambdaWrapper<Article>();
        lambdaWrapper.selectAll(Article.class)
                .selectAll(Cate.class)
                .leftJoin(Cate.class,Cate::getId,Article::getCateid)
                .eq(cateid != null, Article::getCateid,cateid)
                .like(title != null,Article::getTitle,title)
                .between(start_time != null && end_time != null,Article::getCreate_date,start_time,end_time);
        Page articlePage = new Page<>(page,limit);
        articleService.pageMaps(articlePage,lambdaWrapper);
        System.out.println("articlePage = " + articlePage);
        return ResultData.ok("",PageResult.init(articlePage));
    }

    /**
     * @Author 16372
     * @Description //TODO 查询文章详情 未经测试
     * @Date 14:23 2022/2/26
     * @Param [id]
     * @return com.itheima.ydd.common.ResultData
     **/
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id){
        Article article = articleService.getById(id);
        return ResultData.ok("",article);
    }

    /**
     * @Author 16372
     * @Description //TODO 修改文章 未经测试
     * @Date 14:24 2022/2/26
     * @Param [article]
     * @return com.itheima.ydd.common.ResultData
     **/
    @PostMapping("/update")
    public ResultData update(@RequestBody Article article){
        articleService.updateById(article);
        return ResultData.ok("","");
    }

}
